<?php
/**
* user.php
*
* Create on 2015-8-17
* Create by TT
*/
namespace haibao\app\business;

class Account extends \haibao\app\business\Base {

    /**
     * @var $_instance
     */
    private static $_instance;
    
    /**
     * instance()
     */
    private static function instance(){
        if(!self::$_instance){
            self::$_instance = new \haibao\app\data\Account();
        }
        return self::$_instance;
    }
    
    /**
     * 获取消息列表
     */
	public static function setMessageList($userId, $systemType = null){
		return self::instance()->setMessageList($userId, $systemType);
	}
	
	/**
	 * 得到app用户未读消息数
	 */
	public static function getUnreadMessageNum($userId, $systemType = null){
		if(empty($userId)){
			throw new BusinessException('参数不正确',\haibao\app\business\Oauth::DATA_ERROR_CODE_EMPTY);
		}
		return (int)self::instance()->getUnreadMessageNum($userId, $systemType);
	}
	
	public static function setMessageIsRead($userId,$messageId){
		if(empty($userId) || empty($messageId)){
			throw new BusinessException('参数不正确',\haibao\app\business\Oauth::DATA_ERROR_CODE_EMPTY);
		}
		self::instance()->setMessageIsRead($userId,$messageId);
	}
	
	public static function deleteMessage($userId, $messageIds){
	    if(empty($userId) || empty($messageIds)){
	        throw new BusinessException('参数不正确',\haibao\app\business\Oauth::DATA_ERROR_CODE_EMPTY);
	    }
	    $msgArr = array_filter(array_unique(explode(',', $messageIds)));
	    self::instance()->deleteMessage($userId, $msgArr);
	}
	
	public static function getPreAdvertList($adIdArray){
	    $retAdvert = array();
	    $advertList = self::instance()->getPreAdvertList($adIdArray);
	    if($advertList){
	        foreach($advertList as $key=>$advert){
	            if($advert['weight'] == 0){
	                unset($advertList[$key]);
	                continue;
	            }
	            $adv = array();
	            $adv['adId'] = $advert['interface_banner_id'];
	            $adv['zoneId'] = $advert['zone_id'];
	            $adv['picUrl'] = 'http://cdn0.haibao.cn/store/media/'.$advert['filename'];
	            preg_match_all('/href=\"javascript:;\">(.*)<\/a>/isU', $advert['bannertext'], $result);
	            $adv['title'] = join('###', $result[1]);
	            if($advert['url'] == 'http://'){
	                $adv['adUrl'] = '';
	            }else{
	                $adv['adUrl'] = self::getAdUrl($advert['interface_banner_id'], $advert['zone_id'], $advert['url']);
	            }
	            if($advert['pv_detection']){
	                preg_match('/<img src=\"(.*)\" width=0/isU', $advert['pv_detection'], $result);
	                $adv['pvMonitor'] = $result[1];
	            }else{
	                $adv['pvMonitor'] = null;
	            }
	            $adv['bmsPvMonitor'] = self::getBmsPvMonitor($advert['interface_banner_id'], $advert['interface_campaign_id'], $advert['zone_id']);
	            array_push($retAdvert, $adv);
	        }
	    }
	    return $retAdvert;
	}
	
	public static function getAdvertList($adIdArray) {
	    $retAdvert = array();
	    $advertList = self::instance()->getAdvertList($adIdArray);
	    if($advertList){
	        foreach($advertList as $key=>$advert){
	            if($advert['weight'] == 0){
	                unset($advertList[$key]);
	                continue;
	            }
	            $adv = array();
	            $adv['adId'] = $advert['interface_banner_id'];
	            $adv['zoneId'] = $advert['zone_id'];
	            $adv['picUrl'] = 'http://cdn0.haibao.cn/store/media/'.$advert['filename'];
	            preg_match_all('/href=\"javascript:;\">(.*)<\/a>/isU', $advert['bannertext'], $result);
	            $adv['title'] = join('###', $result[1]);
	            if($advert['url'] == 'http://'){
	                $adv['adUrl'] = '';
	            }else{
	                $adv['adUrl'] = self::getAdUrl($advert['interface_banner_id'], $advert['zone_id'], $advert['url']);
	            }
	            if($advert['pv_detection']){
                    preg_match('/<img src=\"(.*)\" width=0/isU', $advert['pv_detection'], $result);
                    $adv['pvMonitor'] = $result[1];
	            }else{
	                $adv['pvMonitor'] = null;
	            }
	            $adv['bmsPvMonitor'] = self::getBmsPvMonitor($advert['interface_banner_id'], $advert['interface_campaign_id'], $advert['zone_id']);
	            array_push($retAdvert, $adv);
	        }
	    }
	    return $retAdvert;
	}
	
	private static function getAdUrl($adid, $zoneId, $url){
	    if(empty($url)){
	        return null;
	    }
	    return sprintf('http://ad.haibao.cn/ck.php?oaparams=2__bannerid=%s__zoneid=%s__cb=%s__oadest=%s', $adid, $zoneId, uniqid(), urlencode($url));
	}
	
	private static function getBmsPvMonitor($adid, $clientid, $zoneId){
		return sprintf('http://ad.haibao.cn/lg.php?zones=%s,%s&cb=%s', $adid, $zoneId, uniqid());
	}
}